<?php
	session_start();

	require_once("includes/connect.php");
	require_once("includes/function.php");

	$pet_id = $_REQUEST["pet_id"];

	$db_edit = NEW myDBS;
	$db_edit->selectDB("account");
	$sql ="SELECT DISTINCT panngan2_id, panngan3_id, budget_year, month_year, ex1_id,
			ex2_id, ex3_id
			FROM tbl_salary WHERE pet_id = '$pet_id'";
	$result = $db_edit->runSQL($sql);
	$rows = $db_edit->fetch_object ( $result );

	$panngan2 = $rows->panngan2_id;
	$panngan3 = $rows->panngan3_id;

	$budget_year = $rows->budget_year;

	$month_year = $rows->month_year;
	
	$ex1_id = $rows->ex1_id;
	$ex2_id = $rows->ex2_id;
	$ex3_id = $rows->ex3_id;
	
?>

<script type="text/javascript">
	$(document).ready(function(){

		$.post('dropdown.php', {  tablename: 'expenses_lvl1', dd: 'ex1_id', idname: 'expenses1_id', fieldname: 'expenses1_name', by: '03', ex1_id:'<?php echo $ex1_id; ?>', ex2_id:'<?php echo $ex2_id; ?>', ex3_id:'<?php echo $ex3_id; ?>'  }, function(data) { $("#expenses1").html(data); } );

		$("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
			submitSuccess: function ($form, event) { 
				//event.preventDefault();
			}
		});

		$( "#panngan2" ).change(function() {
			var panngan2= $(this).val();
			var panngan3 = "<?php echo $panngan3; ?>";
			
			//alert(panngan2);
			
			if (panngan2 != "") {
				//alert(panngan2);
				$.post('dd_panngan.php', {  panngan2: panngan2, panngan3: panngan3}, function(data) { 
					$("#dd_panngan3").html(data); 

				} );
			}

		});

		$("#month_year").datepicker({

			dateFormat: "yy-mm"
			//defaultDate: new Date()
			//showMonthAfterYear:true,
			
		});
		
		$('#panngan2').trigger('change');

		$("#save_salary").click(function() {
			event.preventDefault();
			
			var array1 = [];
			$("input[id^='amount']").each(function(i){
			  array1[i] = $(this).val();
			  
			  //alert(array1[i]);
			});

			var panngan2 = $("#panngan2").val();
			var panngan3 = $("#panngan3").val();
			var month_year = $("#month_year").val();
			var budget_year = $("#budget_year").val();
			var ex1_id = $("#ex1_id").val();
			var ex2_id = $("#ex2_id").val();
			var ex3_id = $("#ex3_id").val();

			$.post('process_petition_salary.php', {  panngan2: panngan2, panngan3: panngan3, month_year: month_year, budget_year:budget_year, ex1_id:ex1_id, ex2_id:ex2_id, ex3_id:ex3_id, array1: array1, task:'edit', pet_id:'<?php echo $pet_id; ?>'}, function(data) { 
				//alert(data);
				//$("#test").html(data);
				window.location.href = "index.php?pages=frm_petition_salary&pageid=1";
				}
			);
						
		});
		
	}); //(document).ready

	
</script>
<div id="test"></div>
<form id="frm_petition_salary_edit" name="frm_petition_salary_edit" method="post" action="index.php" novalidate="">
	<input type="hidden" name="pages" value="frm_petition_salary_edit">
	<input type="hidden" name="pageid" value="2">
	<div class="row-fluid">
		<div class="control-group span2">
			<label class="control-label" for="budget_year">ปีงบประมาณ</label>
			<div class="controls ">
				<select class="span12" id="budget_year" name="budget_year" readonly>
					<option>2556</option>
					<option>2557</option>
					<option>2558</option>
					<option>2559</option>
					<option>2560</option>
				</select>
			</div>
		</div>

		<div class="control-group span4">
			<label class="control-label" for="panngan">แผนงาน</label>
			<div class="controls ">
				<select class="span12" id="panngan2" name="panngan2" required>
					<option value="">เลือก...</option>
				<?php


					$db = NEW myDBS;
					$db->selectDB("hr_udoncity");
					
					$sql = "SELECT DISTINCT panngan2_id 
								FROM position 
								WHERE lvl_id like '" .$_SESSION["lvl1_id"]. "%'
								AND panngan2_id IS NOT NULL"
								; 

					//echo $sql;

					$result = $db->runSQL($sql);
					
					while($rows = $db->fetch_object ( $result )){
						$db->selectDB("account");
						$sql = "SELECT DISTINCT * FROM panngan2 WHERE panngan2_id = '" .$rows->panngan2_id. "'";
						$result1 = $db->runSQL($sql);
						while($rows1 = $db->fetch_object ( $result1 )){
							if ($rows1->panngan2_id == $panngan2) {
								$selected = " selected";

							} else {
								$selected = "";
							}
							echo "<option value='".$rows1->panngan2_id."' $selected>$rows1->panngan2_name</option>";
						}
					}

				?>
				</select>
			</div>
		</div><!--/span-->

		<div class="control-group span4">
			<label class="control-label" for="panngan3">งาน</label>
			<div class="controls " id="dd_panngan3">
				<select class="span12" id="panngan3" name="panngan3">
					<option value="">เลือก</option>

				</select>
			</div>
		</div><!--/span-->

		<div class="control-group span2">
			<label class="control-label" for="month_year">เดือน/ปี</label>
			<div class="controls">
				<input class="span12" type="text" id="month_year" name="month_year" value="<?php echo $month_year; ?>" required />
            </div>
		</div>
	</div><!--/row-fluid-->

	<div class="row-fluid">
		<div class="control-group span4">
			<label class="control-label" for="expenses1_name">หมวด / ลักษณะ</label>
			<div id="expenses1" class="controls">
				<select class="span12" id="ex1_id" name="ex1_id" required>
					<option value="">เลือก...</option>
				</select>
            </div>
		</div>

		<div class="control-group span3">
			<label class="control-label" for="expenses2_name">ประเภท</label>
			<div id="expenses2" class="controls">
				<select class="span12" id="ex2_id" name="ex2_id" required>
					<option value="">เลือก...</option>
				</select>
            </div>
		</div>

		<div class="control-group span3">
			<label class="control-label" for="expenses3_name">รายการ</label>
			<div id="expenses3" class="controls">
				<select class="span12" id="ex3_id" name="ex3_id" required>
					<option value="">เลือก...</option>
				</select>
            </div>
		</div>

		
	</div><!--/row-fluid-->


<?php
		$sql = "SELECT salary_id, id, salary
				FROM  tbl_salary
				WHERE  pet_id =  '$pet_id'
				ORDER BY id
				";
		//echo $sql;
		$db->selectDB("account");
		$results = $db->runSQL($sql);
?>
	<div id="test"></div>
	<table id="example" class="table table-bordered" width="100%" >
		<caption><br></caption>
		<thead>
			<tr>
				<th>#</th>
				<th>เลขประจำตัว</th>
				<th>ชื่อ - นามสกุล</th>
				<th>ตำแหน่ง</th>
				<th>จำนวน</th>
				
			</tr>
		</thead>
		<tbody>
<?php
		$i=0;
		$db1 = NEW myDBS;
		$db1->selectDB("hr_udoncity");

		while ($rows = $db->fetch_object($results)) {
			$sql = "SELECT fname, lname, position_name FROM person, position
					WHERE position.id = person.id
					AND person.id = '". $rows->id ."'
					";
			$results1 = $db1->runSQL($sql);
			//echo $sql;
			$rows1 = $db1->fetch_object($results1);
			$i++;
			echo "<tr>
						<td>$i</td>
						<td>$rows->id</td>
						<td>$rows1->fname&nbsp;&nbsp;&nbsp;$rows1->lname</td>
						<td>$rows1->position_name</td>
						<td align='right'><input type='text' class='span3' id='amount$i' name='amount$i' value='$rows->salary'></td>";
			echo "</tr>";

		}
?>
	</tbody>
	</table>

	<p class="pull-right">

	<button id="save_salary" name = "save_salary" class="btn btn-primary"><i class="icon-ok icon-white"></i>&nbsp;บันทึกฏีกาเงินเดือน</button>
	<a href="index.php?pages=frm_petition_salary&pageid=1" role="button" class="btn"><i class="icon-remove"></i>&nbsp;ยกเลิก</a>
	</p>
</form> 
